package hit.example.Db;

import java.util.ArrayList;

import hit.example.Object.ObjWord;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbRecentFav extends SQLiteOpenHelper {
	public static final String KEY_ID = "_id";
	public static final String KEY_WORD = "word";
	public static final String KEY_CONTENT = "content";

	private SQLiteDatabase mDB;
	private ObjWord obj;

	public static final String CREATE_TABLE_tblRecent = "create table tblRecent (_id integer primary key autoincrement,word text not null,content not null);";
	public static final String CREATE_TABLE_tblFavor = "create table tblFavor (_id integer primary key autoincrement,word text not null,content not null);";
	public static final String DATABASE_NAME = "dbRecentFav";
	public static final String TABLENAME_RECENT = "tblRecent";
	public static final String TABLENAME_FAVOR = "tblFavor";
	private static final int DATABASE_VERSION = 2;

	public DbRecentFav(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_TABLE_tblFavor);
		db.execSQL(CREATE_TABLE_tblRecent);
	}

	public void CloseDB() {
		mDB.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLENAME_FAVOR);
		db.execSQL("DROP TABLE IF EXISTS " + TABLENAME_RECENT);
		onCreate(db);
	}

	public long createRecent(ObjWord obj) {
		mDB = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_WORD, obj.getiWord());
		contentValues.put(KEY_CONTENT, obj.getiContent());
		//Log.i("aaaaaaaaaaaaaaaa", obj.getiWord());
		return mDB.insert(TABLENAME_RECENT, null, contentValues);
	}

	public long createFavor(ObjWord obj) {
		mDB = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_WORD, obj.getiWord());
		contentValues.put(KEY_CONTENT, obj.getiContent());
		//Log.i("aaaaaaaaaaaaaaaa", obj.getiWord());
		return mDB.insert(TABLENAME_FAVOR, null, contentValues);
	}

	public ArrayList<ObjWord> getlistRecent() {
		mDB = this.getReadableDatabase();
		ArrayList<ObjWord> listRecentWord = new ArrayList<ObjWord>();
		String sql;
		sql = "SELECT * FROM '" + TABLENAME_RECENT + "' ORDER BY " + KEY_ID
				+ " DESC";
		Cursor c = mDB.rawQuery(sql, null);
		int iID = c.getColumnIndex(KEY_ID);
		int iWord = c.getColumnIndex(KEY_WORD);
		int iContent = c.getColumnIndex(KEY_CONTENT);
		if (c.moveToFirst()) {
			do {
				obj = new ObjWord();
				obj.setiD(iID);
				obj.setiWord(c.getString(iWord));
				obj.setiContent(c.getString(iContent));
				listRecentWord.add(obj);
				//Log.i("aaaaaaaaaaaaaaaa", obj.getiContent());
			} while (c.moveToNext());
		}
		CloseDB();
		return listRecentWord;

	}

	public ArrayList<ObjWord> getlistFavor() {
		mDB = this.getReadableDatabase();
		ArrayList<ObjWord> listFavorWord = new ArrayList<ObjWord>();
		String sql;
		sql = "SELECT * FROM '" + TABLENAME_FAVOR + "' ORDER BY " + KEY_ID
				+ " DESC";
		Cursor c = mDB.rawQuery(sql, null);
		int iID = c.getColumnIndex(KEY_ID);
		int iWord = c.getColumnIndex(KEY_WORD);
		int iContent = c.getColumnIndex(KEY_CONTENT);
		if (c.moveToFirst()) {
			do {
				obj = new ObjWord();
				obj.setiD(iID);
				obj.setiWord(c.getString(iWord));
				obj.setiContent(c.getString(iContent));
				listFavorWord.add(obj);
				//Log.i("aaaaaaaaaaaaaaaa", obj.getiContent());
			} while (c.moveToNext());
		}
		CloseDB();
		return listFavorWord;
	}
	public int getFavCount(String word) {
		mDB = this.getReadableDatabase();
		String sql = "SELECT  * FROM " + TABLENAME_FAVOR + " WHERE word='"
				+ word + "'";

		Cursor cursor = mDB.rawQuery(sql, null);
		cursor.moveToFirst();
		cursor.close();
		return cursor.getCount();
	}
	public Boolean delAllRecentWord(){
		mDB = this.getReadableDatabase();
		mDB = this.getWritableDatabase();
		return mDB.delete(TABLENAME_RECENT, null, null)>0;
	}
	public Boolean delAllFavorWord(){
		mDB = this.getReadableDatabase();
		mDB = this.getWritableDatabase();
		return mDB.delete(TABLENAME_FAVOR, null, null)>0;
	}
	public Boolean delThisWordRecent(ObjWord obj){
		mDB = this.getReadableDatabase();
		mDB = this.getWritableDatabase();
		return mDB.delete(TABLENAME_RECENT,KEY_WORD+"="+"'"+obj.getiWord()+"'",null)>0;
	}
	public Boolean delThisWordFavor(ObjWord obj){
		mDB = this.getReadableDatabase();
		mDB = this.getWritableDatabase();
		return mDB.delete(TABLENAME_FAVOR,KEY_WORD+"="+"'"+obj.getiWord()+"'",null)>0;
	}
	
}
